package mh.demo.ehcache;

import mh.demo.PrintUtils;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.Configuration;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.xml.XmlConfiguration;

import java.net.URL;
import java.util.Map;
import java.util.TreeMap;

public class EhcacheDemoXml01 {

    public static void main(String[] args) {
        URL myUrl = EhcacheDemoXml01.class.getClassLoader().getResource("ehcache-demo-xml-01.xml");
        Configuration xmlConfig = new XmlConfiguration(myUrl);
        CacheManager cacheManager = CacheManagerBuilder.newCacheManager(xmlConfig);
        cacheManager.init();
        Cache<Long, String> cache = cacheManager.getCache("bar", Long.class, String.class);
        for (long i = 0; i < 5; i++) {
            cache.put(i, "bar" + i);
        }
        Map<Long, String> treeMap = new TreeMap<>();
        for (Cache.Entry<Long, String> e : cache) {
            treeMap.put(e.getKey(), e.getValue());
        }
        PrintUtils.println(treeMap);

        cacheManager.removeCache("myCache");  //（9）
        cacheManager.close();
    }

}
